package way.learning.zz.FtpUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FtpFileMonitor {

    private static final long interval = 1 * 20 * 1000;

    //轮询次数计数
    private static int count = 0;

    private static Date previouseTime;

    private static List<String> previousList;

    private static List<String> currentList;

    private static List<String> newAddList;

    private static FTPClient ftpClient;

    private static FtpFileMonitor ftpFileMonitor;



    //构造函数
    private FtpFileMonitor() {
        ftpClient = new FTPClient();
        previousList = new ArrayList<String>();
        currentList = new ArrayList<String>();
        newAddList = new ArrayList<String>();
    }

    public static FtpFileMonitor getInstance() {
        if (ftpFileMonitor == null) {
            ftpFileMonitor = new FtpFileMonitor();
        }

        return ftpFileMonitor;
    }


    //连接登录到FTP
    public boolean login(String host, int port, String username, String password) {

        try {
            ftpClient.connect(host, port);
            ftpClient.enterLocalPassiveMode();

            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                ftpClient.disconnect();
                throw new IOException("Exception when connecting to FTP Server: " + ftpClient);
            }

            //登录远程ftp服务器
            return ftpClient.login(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    //轮询目录得到新增文件
    public List<String> getCurrentFileList(String dir) throws IOException {
        FTPFile[] ftpFiles = ftpClient.listFiles(dir);

        for (FTPFile ftpFile : ftpFiles) {
            currentList.add(ftpFile.getName());
        }

        return currentList;
    }

    //判断时间是否满足
    public static boolean isSatisfy() {
        Date currentTime = new Date();

        if (currentTime.getTime() - previouseTime.getTime() >= interval)
            return true;
        else
            return false;
    }

    //开始监测
    public void beginMonitor(String host, int port, String username, String password, String workingDirectory) throws IOException {

        //如果连接服务器成功
        if (login(host, port, username, password)) {

            for (; ;) {
                count++;
                getCurrentFileList(workingDirectory);

                //输出测试
                System.out.println("第" + count + "次轮询，当前目录下的文件有：" + JSON.toJSONString(currentList));


                newAddList.addAll(ListUtils.subtract(currentList, previousList));


                //输出测试
                System.out.println("第" + count + "次轮询下，当前目录新增的的文件有：" + JSON.toJSONString(newAddList));


                previousList.clear();
                previousList.addAll(currentList);

                //添加到数据库记录
                String source = null;
                String dayStr = null;
                if(username.equals("ndycfd3")){
                    source = "dianxin";
                    dayStr = workingDirectory.substring(workingDirectory.length()-6,workingDirectory.length());
                }else if(username.equals("ndycfd2")){
                    source = "mobile";
//                    dayStr = DateTimeUtils.getTodayStr();
                }

                if (source == null || dayStr == null){
                    System.out.println("source或者daystr不能为空");
                    return;
                }


                //清空列表
                currentList.clear();
                newAddList.clear();


                //执行完一次轮询后
                previouseTime = new Date();
                System.out.println("执行完第" + count + "次轮询时的时间为：" + previouseTime);

                if (isSatisfy())
                    continue;
                else {
                    while (!isSatisfy()) {

                        //do nothing
                    }
                }
            }
        }
    }


    public static void main(String[] args) throws IOException {
        FtpFileMonitor ftpFileMonitor = getInstance();



        //服务器信息
        String host = "119.23.14.212";
        int port = 21001;
        String username = "ndycfd2";
        String username1 = "ndycfd3";

        String password = "1Q3qKcr";
        String password1 = "dSqD5e1";
        String workingDirectory = "/";

        ftpFileMonitor.beginMonitor(host,port,username,password,workingDirectory);
    }
}